﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using LeanBoard.Models;

namespace LeanBoard.HtmlBuilders
{
    public class ItemHtmlBuilder
    {
        private ProfilesConfigSection _profilesConfigSection;
        private ProfilesElementCollection Profiles
        {
            get
            {
                if (_profilesConfigSection == null)
                {
                    _profilesConfigSection = (ProfilesConfigSection)ConfigurationManager.GetSection("profilesSection");
                }
                if (_profilesConfigSection == null)
                {
                    return new ProfilesElementCollection();
                }
                return _profilesConfigSection.Profiles;
            }
        }

        private TeamsConfigSection _teamsConfigSection;
        private TeamsElementCollection Teams
        {
            get
            {
                if (_teamsConfigSection == null)
                {
                    _teamsConfigSection = (TeamsConfigSection)ConfigurationManager.GetSection("teamsSection");
                }
                if (_teamsConfigSection == null)
                {
                    return new TeamsElementCollection();
                }
                return _teamsConfigSection.Teams;
            }
        }

        public string BuildBoardItemsHtml(IList<Item> items)
        {
            string html = string.Empty;

            foreach (Item item in items)
            {
                //if (item.ItemType == null)
                //    item.ItemType = "change";

                string headerClass = item.ItemType.ToLower() + "Header";
                string progressClass = item.ItemType.ToLower() + "Progress";
                string bodyClass = item.ItemType.ToLower() + "Body";

                html += String.Format("<li class='{0}' id='{1}'>" + Environment.NewLine, "item " + bodyClass, item.Id);
                html += String.Format("<div class='{0}'>" + Environment.NewLine, "handle " + headerClass);
                html += String.Format("<div class='{0}' style='width:{1}%;'></div>" + Environment.NewLine, "progress " + progressClass, item.Progress);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemPriority", "!" + item.PriorityString);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemNumber", item.Id);
                html += "</div>" + Environment.NewLine;
                html += String.Format("<div class='{0}'>", "content");
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "crResponsible " + bodyClass, item.CurrentlyResponsible);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "crTitle " + bodyClass, item.Title);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemUnid", item.Unid);
                html += "</div>" + Environment.NewLine;
                html += "</li>" + Environment.NewLine;
                html += Environment.NewLine;
            }

            return html;
        }

        public string BuildStandupItems(ItemsCollection items)
        {
            List<Item> forStandup = new List<Item>();
            forStandup.AddRange(items.Failed);
            forStandup.AddRange(items.Development);
            forStandup.AddRange(items.ReadyForCodeReview);
            forStandup.AddRange(items.CodeReview);
            forStandup.AddRange(items.ReadyForApporval);
            forStandup.AddRange(items.Testing);

            return BuildOverlayStandupItemsHtml(forStandup);
        }

        public string BuildAllItems(ItemsCollection items)
        {
            List<Item> forStandup = new List<Item>();
            forStandup.AddRange(items.Scheduled);
            forStandup.AddRange(items.Analysis);
            forStandup.AddRange(items.AnalysisReadyForReview);
            forStandup.AddRange(items.AnalysisReview);
            forStandup.AddRange(items.AnalysisDone);
            forStandup.AddRange(items.ReadyForDevelopment);
            forStandup.AddRange(items.Failed);
            forStandup.AddRange(items.Development);
            forStandup.AddRange(items.ReadyForCodeReview);
            forStandup.AddRange(items.CodeReview);
            forStandup.AddRange(items.ReadyForApporval);
            forStandup.AddRange(items.Testing);
            forStandup.AddRange(items.Done);

            return BuildOverlayAllItemsHtml(forStandup);
        }

        public string BuildResponsiblesGallery(string physicalApplicationPath)
        {
            string html = string.Empty;

            html += "<div>" + Environment.NewLine;
            foreach (ProfilesElement profile in Profiles)
            {
                html += GetGalleryImageHtml(profile);
            }
            html += "</div>" + Environment.NewLine;

            return html;
        }

        private static string GetGalleryImageHtml(ProfilesElement profile)
        {
            string html = string.Empty;
            html += String.Format("<div class='{0}'>" + Environment.NewLine, "profileImage");
            html += String.Format("<img src='{0}' alt='' />" + Environment.NewLine, "/ProfileImage/GetImage/?imageProfile=" + profile.ImageName);
            html += String.Format("<h2>{0}</h2>" + Environment.NewLine, profile.DisplayName);
            html += String.Format("<img src='{0}' alt='' />" + Environment.NewLine, "/ProfileImage/GetCountryImage/?countryName=" + profile.Country + ".png");
            html += "</div>" + Environment.NewLine;

            return html;
        }

        private string BuildOverlayStandupItemsHtml(IEnumerable<Item> forStandup)
        {
            string html = string.Empty;

            foreach (Item item in forStandup)
            {
                html += GetSingleItemHtml(item, "standup");
            }

            return html;
        }

        private string BuildOverlayAllItemsHtml(IEnumerable<Item> forAllItems)
        {
            string html = string.Empty;

            foreach (Item item in forAllItems)
            {
                html += String.Format("<div class='{0}' id='{1}'>" + Environment.NewLine, "apple_overlay " + item.ItemType.ToLower() + "Body", "singleOverlay" + item.Unid);
                html += String.Format("<div class='{0}'>Overview</div>", "button overview");
                html += String.Format("<div class='{0}'>Details</div>", "button details");
                html += String.Format("<div class='{0}'>Close</div>", "button close");
                html += GetSingleItemHtml(item, "all");
                html += String.Format("<div class='{0}'></div>", "externalContentFrame");
                html += "</div>" + Environment.NewLine;
            }

            return html;
        }

        private string GetSingleItemHtml(Item item, string idPrefix)
        {
            string html = string.Empty;
            html += String.Format("<div class='{0}' id='{1}'>", "itemPopup", idPrefix + item.Id);

            html += String.Format("<div class='{0}'>", "itemInfo");
            html += String.Format("<div>{0} {1} - {2}</div>", item.Type, item.Id, item.Status);
            html += String.Format("<div>Priority: {0}</div>", item.PriorityString);
            html += String.Format("<div>Done: {0}%</div>", item.Progress);
            html += "</div>";

            html += "<div style='float: right;' class='profileImage'>";
            html += String.Format("<img src='{0}' alt='' width='178' height='178'>", "/ProfileImage/GetImage/?imageProfile=" + (Profiles[item.CurrentlyResponsible] == null ? "Anonymous.jpg" : Profiles[item.CurrentlyResponsible].ImageName));
            html += String.Format("<h2>{0}</h2>", item.CurrentlyResponsible);
            html += "</div>";
            html += String.Format("<div class='{0}'>{1}</div>", "itemTitle", item.Title);
            html += String.Format("<div class='{0}'>{1}</div>", "itemUnid", item.Unid);

            html += "</div>";
            html += Environment.NewLine;

            return html;
        }

        public string BuildTeamUrls(bool readOnly = false)
        {
            string html = string.Empty;
            string viewName = readOnly ? "ReadOnly" : "Index";

            foreach (TeamsElement team in Teams)
            {
                string teamScheduledReleseStr = (team.ScheduledRelease == "") ? team.ScheduledRelease : "/" + team.ScheduledRelease;
                html += String.Format("<a id='{0}' class='{1}' href='{2}'>{3}</a>", team.TeamName, "button", "/leanboard/" + viewName + "/" + team.TeamName + teamScheduledReleseStr, team.DisplayName);
            }
            html += String.Format("<a class='{0}' href='{1}'>{2}</a>", "button", "/leanboard/" + viewName + "/", "All Items");
            html += Environment.NewLine;

            return html;
        }

        public string GetTeamDisplayName(string teamName, string release)
        {
            string releaseString;
            if (release != null && release != "")
            {
                releaseString = " (" + release + ")";
            }
            else
            {
                releaseString = string.Empty;
            }

            if (teamName == null)
            {
                return "Whiteboard";
            }

            foreach (TeamsElement team in Teams)
            {
                if (team.TeamName.ToLower() == teamName.ToLower())
                    return team.DisplayName + releaseString;
            }

            return teamName + " (undefined)" + releaseString;
        }

        public string GetWip(string teamName)
        {
            foreach (TeamsElement team in Teams)
            {
                if (team.TeamName == teamName)
                    return team.Wip.ToString();
            }
            if (teamName == null)
            {
                return GetSummarizedWipForAllTeams().ToString();
            }
            return "(undefined)";
        }

        private int GetSummarizedWipForAllTeams()
        {
            return Teams.Cast<TeamsElement>().Sum(teamsElement => teamsElement.Wip);
        }
    }
}
